home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <dos.h>
- #include <limits.h>
-
- #define LASTLINE 23
-
- #define LOWER_LEFT 0x1800
- #define LOWER_RIGHT 0x184f
-
- int lineno = 1;
- #ifdef L_EX /* Long */
- #ifdef U_EX /* Unsigned long */
- char top_line[] = "Test atoul_ex";
- int atoul_ex(char **, unsigned long *);
- #else /* Signed long */
- char top_line[] = "Test atol_ex";
- int atol_ex(char **, long *);
- #endif /* #ifdef U_EX */
- #else /* Standard length */
- #ifdef U_EX /* Unsigned int */
- char top_line[] = "Test atou_ex";
- int atou_ex(char **, unsigned *);
- #else /* Signed int */
- char top_line[] = "Test atoi_ex";
- int atoi_ex(char **, int *);
- #endif /* #ifdef U_EX */
- #endif /* #ifdef L_EX */
-
- /* Clear screen */
- void cls()
-
- {
- static union REGS in = {0x600, 0, 0, LOWER_RIGHT};
- union REGS out;
-
- (void) int86(0x10, &in, &out);
- return;
- }
-
- /* Set cursor to (x, y) */
- void curset(y, x, page)
- int y, x, page;
-
- {
- union REGS in, out;
-
- in.x.ax = 0x200;
- in.x.bx = 0;
- in.x.dx = (y << 8) + x;
- (void) int86(0x10, &in, &out);
- return;
-
- }
-
-
- void clr_r24()
-
- {
-
- union REGS in, out;
-
- curset(24, 0, 0);
-
- in.x.ax = 0x600;
- in.x.bx = 0;
- in.x.cx = LOWER_LEFT;
- in.x.dx = LOWER_RIGHT;
- (void) int86(0x10, &in, &out);
- return;
- }
-
- /* Get a line from stdin to buffer, max_char characters maximum */
- void egets(buffer, max_char)
- char *buffer;
- int max_char;
-
- {
- union REGSS sin, sout;
- char inbuff[256];
-
- inbuff[0] = max_char;
- sin.x.ax = 0x0a00;
- makedv(inbuff, &sin.x.dx, &sin.x.ds);
- (void) intdoss(&sin, &sout);
- inbuff[2 + inbuff[1]] = NULL;
- movmem(&inbuff[2], buffer, inbuff[1] + 1);
- return;
-
- }
-
-
- void new_screen()
-
- {
- cls();
- curset(0, (79 - strlen(top_line)) >> 1, 0);
- puts(top_line);
- curset(2, 0, 0);
- printf("%15s%15s%4s %s\n\n", "Input String", "Number", "Ofs", "Result");
- lineno = 5;
- }
-
- void header()
-
- {
- clr_r24();
- fputs("Press any key to continue ", stdout);
- (void) getch();
- new_screen();
- }
-
- int test_atov_ex(p, end_char)
- char **p;
- char *end_char;
-
- {
- static char *result_name[] = {"End of non-empty string",
- "Non-numeric character after number",
- "Empty input string",
- "No digits before non-numeric character",
- "Overflow"};
-
- int result;
- char *p1, p_disp[18];
- #ifdef L_EX /* Long */
- #ifdef U_EX /* Unsigned long */
- unsigned long number;
- #define ATOV_EX atoul_ex
- #define NFORM "%17s%15lu"
- #else /* Signed long */
- long number;
- #define ATOV_EX atol_ex
- #define NFORM "%17s%15ld"
- #endif /* #ifdef U_EX */
- #else /* Standard length */
- #ifdef U_EX /* Unsigned int */
- unsigned number;
- #define ATOV_EX atou_ex
- #define NFORM "%17s%15u"
- #else /* Signed int */
- int number;
- #define ATOV_EX atoi_ex
- #define NFORM "%17s%15d"
- #endif /* #ifdef U_EX */
- #endif /* #ifdef L_EX */
- int term_off;
-
- (void) strcpy(p_disp, "\"");
- (void) strcat(strcat(p_disp, *p), "\"");
-
- p1 = *p;
- if (lineno > LASTLINE)
- header();
- curset(lineno - 1, 0, 0);
- result = ATOV_EX(&p1, &number);
- *end_char = *p1;
- if ((result != 3) || /* Not a non-numeric string */
- (toupper(*end_char) != 'Q'))
- {
- printf(NFORM, p_disp, number);
- term_off = p1 - *p;
- printf("%4d %d (%s)\n", term_off, result, result_name[result]);
- }
- ++lineno;
- return(result);
- }
-
- void convert_and_back(integer)
- #ifdef L_EX /* Long */
- #ifdef U_EX /* Unsigned long */
- #define STC stcul_d
- unsigned long integer;
- #else /* Signed long */
- #define STC stcl_d
- long integer;
- #endif /* #ifdef U_EX */
- #else /* Standard length */
- #ifdef U_EX /* Unsigned int */
- #define STC stcu_d
- unsigned integer;
- #else /* Signed int */
- #define STC stci_d
- int integer;
- #endif /* #ifdef U_EX */
- #endif /* #ifdef L_EX */
-
- {
- static char s[16];
- static char *ps1 = s;
- static char **ps2 = &ps1;
- char last_char;
-
- STC(s, integer);
- test_atov_ex(ps2, &last_char);
- }
-
- main()
- {
- char str_number[16];
- char *p0, **p00, term_char;
- int i, again;
- #ifdef L_EX /* Long */
- #ifdef U_EX /* Unsigned long */
- #define MAX_VALUE ULONG_MAX
- static unsigned long power_of_2 = 0;
- #else /* Signed long */
- #define MAX_VALUE LONG_MAX
- static long power_of_2 = 0;
- #endif /* #ifdef U_EX */
- #else /* Standard length */
- #ifdef U_EX /* Unsigned int */
- #define MAX_VALUE UINT_MAX
- static unsigned power_of_2 = 0;
- #else /* Signed int */
- #define MAX_VALUE INT_MAX
- static int power_of_2 = 0;
- #endif /* #ifdef U_EX */
- #endif /* #ifdef L_EX */
-
- #ifdef U_EX /* Unsigned */
- static char *over_type[] = {"Third shift", "Second add"};
- static int n_over = 2;
- #ifdef L_EX /* Unsigned long */
- static char *over_values[] = {"5368709120", "4294967296"};
- #else /* Unsigned int */
- static char *over_values[] = {"81920", "65536"};
- #endif /* #ifdef L_EX */
- #else /* Signed */
- static char *over_type[] = {"Positive", "Negative", "Big"};
- static int n_over = 3;
- #ifdef L_EX /* Signed long */
- static char *over_values[] = {"2147483648", "-2147483649", "4294967296"};
- #else /* Signed int */
- static char *over_values[] = {"32768", "-32769", "65536"};
- #endif /* #ifdef L_EX */
- #endif /* #ifdef U_EX */
-
- new_screen();
-
- convert_and_back(power_of_2); /* "0" */
- while (power_of_2 < MAX_VALUE)
- {
- power_of_2 = (power_of_2 << 1) + 1; /* (2**n) - 1 */
- convert_and_back(power_of_2);
- #ifndef U_EX /* Signed */
- convert_and_back(-power_of_2);
- #endif /* #ifndef U_EX */
- if (power_of_2 < MAX_VALUE) /* MAX_VALUE is always odd */
- convert_and_back(power_of_2 + 1);
- #ifndef U_EX
- convert_and_back((-power_of_2) - 1);
- #endif /* #ifndef U_EX */
- }
-
- p00 = &p0;
- header();
- printf("\nOverflow cases\n");
- lineno += 2;
- for (i = 0; i < n_over; i++)
- {
- printf("\n%s\n", over_type[i]);
- lineno += 2;
- p0 = over_values[i];
- (void) test_atov_ex(p00, &term_char);
- }
-
- puts("\nOther terminating conditions\n\nEmpty string");
- lineno += 4;
- *p0 = NULL;
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 3)
- header();
- lineno += 2;
- puts("\nNumber ends inside string");
- p0 = "0q";
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 3)
- header();
- lineno += 2;
- puts("\nEmpty number inside string");
- p0 = ";";
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 5)
- header();
- lineno += 4;
- puts("\nLeading conditions\n\nOne space");
- p0 = " 1";
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 3)
- header();
- lineno += 2;
- puts("\nTwo leading spaces");
- p0 = " 6";
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 3)
- header();
- lineno += 2;
- puts("\nOne leading space and a plus sign");
- p0 = " +15";
- (void) test_atov_ex(p00, term_char);
-
- if (lineno > LASTLINE - 3)
- header();
- lineno += 2;
- puts("\nLeading plus sign");
- p0 = "+1000";
- (void) test_atov_ex(p00, term_char);
-
- header();
- p0 = str_number;
- again = 1;
-
-
- while (again)
- {
- clr_r24();
- fputs("Enter the string to be tested or q to quit: ", stdout);
- egets(str_number, sizeof str_number);
- if (test_atov_ex(p00, &term_char) == 3) /* No numeric characters */
- again = (toupper(term_char) != 'Q');
- }
- clr_r24(0);
- }